package com.powersi.biz.base.dao;

import java.util.List;

import com.powersi.biz.base.dto.ChooseInstrumentDTO;
import com.powersi.biz.entity.Instrument;
import com.powersi.hygeia.framework.BaseDAOImpl;
import com.powersi.hygeia.framework.util.DAOHelper;
import com.powersi.hygeia.framework.util.DBHelper;
import com.powersi.hygeia.framework.util.UtilFunc;

public class InstrumentDAOImpl extends BaseDAOImpl implements InstrumentDAO{

	private final String tableName = "bs_instrument";
	@Override
	public List queryInstrument() {

		StringBuffer lSQL = new StringBuffer();

		lSQL.append(" select * ");
		lSQL.append("   from BS_INSTRUMENT ");
		lSQL.append("   where 1 = 1 ");

		return DBHelper.executeList(lSQL.toString());
	}

	@Override
	public Instrument query(Long id) {
		StringBuffer lSQL = new StringBuffer();

		lSQL.append(" select * ");
		lSQL.append("   from BS_INSTRUMENT ");
		lSQL.append("   where 1 = 1 ");
		lSQL.append("     and id = "+ id.toString() +"");

		return DBHelper.executeBean(Instrument.class, lSQL.toString());
	}

	@Override
	public int deleteInstrument(Long id) {
		int ret = DBHelper
				.update("update BS_INSTRUMENT set valid_flag = '0' where id = ? and valid_flag = '1'",
						new Object[] { id });
		return ret;
	}

	@Override
	public int updateInstrument(Instrument instrument) {
		return DAOHelper.update(tableName, instrument);
	}

	@Override
	public int saveInstrument(Instrument instrument) {
		return DAOHelper.insert(tableName, instrument);
	}

	@Override
	public List getInstrumentList(ChooseInstrumentDTO chooseInstrumentDTO) {
		StringBuffer strSQL = new StringBuffer();
		strSQL.append(" select * from "+tableName+" bs where 1=1 ");



		String strCond = chooseInstrumentDTO.getQueryCond();
		if (!UtilFunc.isEmptyString(strCond)) {
			strSQL.append(" and (bs.instrument_name like '%"+strCond+"%' or bs.instrument_model like '%"+strCond+"%' or bs.instrument_no like '%"+strCond+"%' ) ");

		}

		String strInstrumentName = chooseInstrumentDTO.getInstrument_name();
		if (!UtilFunc.isEmptyString(strInstrumentName)) {
			strSQL.append(" and bs.instrument_name like '%"+strInstrumentName+"%' ");
		}

		return DBHelper.executeList(strSQL.toString());
	}


}
